package ControlLayer;
import java.util.ArrayList;
import ModelLayer.*;
//model layer package has to be imported

/**
 * Controller class for the address book
 * 
 * @author (DM80 Group 5, Anders, Michael, Jesper, Lars og Kenni) 
 * @version (16/11-2012)
 */
public class AddressCtr
{
    
    private AddressBook addressBook;
    
    //Address container is to be passed as parameter from the main controller
    public AddressCtr()
    {
        addressBook = AddressBook.getAddressBook();
    }
    
    //Dummy implementation: after implementing the model layer 
    //objects of class Person are to be returned, not String
    public String getPerson(int _id)
    {   
        Person person = addressBook.searchById(_id);
        
        String retStr = "";
        
        if(person != null)
        {
            retStr = " ----------------" + "\n";
            retStr += " Person Info:" + "\n";
            retStr += " Id: " + person.getId() + "\n";
            retStr += " Name: " + person.getName() + "\n";
            retStr += " Address: " + person.getAddress() + "\n";
            retStr += " Zipcode: " + person.getZipCode() + "\n";
            retStr += " City: " + person.getCity() + "\n";
            retStr += " Phone: " + person.getPhoneNr() + "\n";
            retStr += " ----------------";
        }
        
        return retStr;
    } 
    
    public Person findPerson(int _id)
    {
        return addressBook.searchById(_id);
    }
    
    public void createPerson(int _id, String _name, String _address, int _zipCode, String _city, String _phoneNr)
    {   
        Person newPerson = new Person(_id, _name, _address, _zipCode, _city, _phoneNr);
        
        addressBook.addPerson(newPerson);
    }
    
    public boolean deletePerson(int _id)
    {
        boolean success = false;
        Person deletePerson = addressBook.searchById(_id);
        
        if(deletePerson != null)
        {
            addressBook.deletePerson(deletePerson);
            
            success = true;
        }
        
        return success;
    }
    
    public boolean updatePerson(int _id, String _name, String _address, int _zipCode, String _city, String _phoneNr)
    { 
        boolean success = false;
        Person person = addressBook.searchById(_id);
        
        if(person != null)
        {
            addressBook.deletePerson(person);
            
            person.setID(_id);
            person.setName(_name);
            person.setAddress(_address);
            person.setZipCode(_zipCode);
            person.setCity(_city);
            person.setPhoneNr(_phoneNr);
            
            addressBook.addPerson(person);
            
            success = true;
        }

        return success;
    }
    
    public String listAllPersons()
    {
        String retStr = "";
        
        ArrayList<Person> allPersons = addressBook.getAllPersons();
        
        if(allPersons != null)
        {
            retStr += "\n";
            retStr += " All Persons:" + "\n";
            
            for(Person person : allPersons)
            {
                retStr += " id: " + person.getId() + " name: " + person.getName() + "\n";         
            }
        }
        
        return retStr;
    }
    
    public ArrayList<Person> getAllPersons()
    {
        return addressBook.getAllPersons();
    }
}
